import db from './db'
import Mock from 'mockjs'
import jwt from 'jsonwebtoken'
import { parseUrl, pick } from '../utlis'

// 读取当前用户信息
Mock.mock('admin/info', 'get', () => {
  let info = {}
  try {
    const token = localStorage.getItem('Oppo-Token')
    //
    info = jwt.decode(token, '3210')
  } catch (err) {
    console.log(err)
  }
  const result = db.select('Oppo_admin', item => {
     return item.username === info.data
  }).map(item =>
    pick(item, ['id', 'avatar', 'username', 'nickname', 'mobile', 'state'])
  )
  return {
    err: 0,
    desc: '执行成功',
    result
  }
})

// 添加
Mock.mock('admin/add', 'post', ({ body }) => {
  const { username, password, nickname = '', mobile, avatar = '', state = 1 } = JSON.parse(body)

  const nameRepeat = db.select('Oppo_admin', item => item.username === username)
  if (nameRepeat && nameRepeat.lenght > 0) return { err: 104, desc: '抱歉，该用户名已经存在' }

  const mobileRepeat = db.select('Oppo_admin', item => item.mobile === mobile)
  if (mobileRepeat && mobileRepeat.lenght > 0) return { err: 104, desc: '抱歉，该手机号已注册' }

  db.add('Oppo_admin', { username, password, nickname, mobile, avatar, state })
  return { err: 0, desc: '执行成功' }
})

// 修改
Mock.mock('admin/edit', 'post', ({ body }) => {
  const { id, username, password, nickname, mobile, avatar, state = 1 } = JSON.parse(body)

  const result = db.select('Oppo_admin', item => item.id === id)[0]
  const { mobile: _mobile, password: _password } = result

  const mobileRepeat = db.select('Oppo_admin', item => item.mobile === mobile)
  if (mobileRepeat && mobileRepeat.lenght > 0) return { err: 104, desc: '抱歉，该手机号已注册' }

  // 如果password有值就修改密码，如果没有就用老密码
  const row = Object.assign(
    { username, nickname, mobile, avatar, state },
    { password: password ? password : _password }
  )

  db.put('Oppo_admin', id, row)
  return { err: 0, desc: '执行成功' }
})
// 审核
Mock.mock('admin/state', 'post', ({ body }) => {
  const { id, state } = JSON.parse(body)
  db.put('Oppo_admin', id, { state })

  return { err: 0, desc: '执行成功' }
})
// 查找
Mock.mock(/admin\/list(\?.+)?/, 'get', ({ url }) => {
  // 分页
  let { state, keyword = '', page = 1, pagesize = 3 } = parseUrl(url)
  state = +state
  //处理中文乱码问题
  keyword = decodeURIComponent(keyword)
  page = +page
  pagesize = +pagesize

  const searchResult = db.select('Oppo_admin', item => {
    return item.username !== 'admin' && (item.state === state || !state) && (
      item.username.includes(keyword) ||
     item.nicnkame.includes(keyword) ||
     item.mobile.includes(keyword) || !keyword
    )
  })
  const total = searchResult.length
  const totalpage = Math.ceil(total / pagesize)
  if (page > totalpage) page = totalpage
  const start = (page - 1) * pagesize
  const end = start + pagesize

  const pageResult = searchResult.slice(start, end)
  return { err: 0, result: pageResult, page, total, pagesize, totalpage, desc: '执行成功' }
})
// 删除
Mock.mock('admin/del', 'post', ({ body }) => {
  const { ids } = JSON.parse(body)
  db.delete('Oppo_admin', ids)
  return {
    err: 0,
    desc: '执行成功'
  }
})
